﻿using QuanLyGiaoVuDAL.Repositories.DatabaseRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuService.CommonServices
{
    public class QuanLyQuyenTruyCapService : IQuanLyQuyenTruyCapService
    {
        public IQuanLyQuyenTruyCapRepository _quyenTruyCapRepository;
        public ITaiLieuRepository _taiLieuRepository;
        public IThamSoRepository _thamSoRepository;

        public QuanLyQuyenTruyCapService(IQuanLyQuyenTruyCapRepository quyenTruyCapRepository, ITaiLieuRepository taiLieuRepository, IThamSoRepository thamSoRepository)
        {
            _quyenTruyCapRepository = quyenTruyCapRepository;
            _taiLieuRepository = taiLieuRepository;
            _thamSoRepository = thamSoRepository;
        }

        public bool KiemTraQuyenTrenThuMuc(Guid maGiangVien, Guid maThuMuc)
        {
            return _quyenTruyCapRepository.KiemTraQuyen(maGiangVien, maThuMuc);
        }


        public bool KiemTraQuyenTrenTaiLieu(Guid maGiangVien, Guid maTaiLieu)
        {
            var taiLieuDTO = _taiLieuRepository.GetTaiLieuById(maTaiLieu);
            return _quyenTruyCapRepository.KiemTraQuyen(maGiangVien, taiLieuDTO.MaThuMuc);
        }


        public void ThietLapQuyen(Guid maGiangVien, Guid maThuMuc)
        {
            _quyenTruyCapRepository.ThietLapQuyen(maGiangVien, maThuMuc);
        }

        public bool LaAdmin(Guid maNguoiDung)
        {
            Guid maAdmin;
            var laAdmin = maNguoiDung.Equals(Guid.TryParse(_thamSoRepository.GetThamSo("MaAdmin"), out maAdmin) ? maAdmin : Guid.Empty);
            return (!maNguoiDung.Equals(Guid.Empty) && laAdmin);
        }
    }
}
